package com.sx.zongzhi.adapter;


import android.content.Context;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.BusinessBean;
import com.sx.zongzhi.bean.PopBusinessRoadBean;
import com.sx.zongzhi.widget.InnerListView;
import com.sx.zongzhi.widget.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XZY on 2017/12/4.
 * 业务信息adapter
 */

public class BusinessTestAdapter extends BaseMultiItemQuickAdapter<BusinessBean,BaseViewHolder> {

    private Context context;
    private List<String> selectList;
    private List<PopBusinessRoadBean> roadBeanList;
    private boolean isSelect;

    public BusinessTestAdapter(Context context,List<BusinessBean> data) {
        super(data);
        this.context=context;
        addItemType(1, R.layout.item_event_type);
        addItemType(2, R.layout.item_business_type);
    }

    public void selectData(List<String> lists,List<PopBusinessRoadBean> roadBeanList){
        selectList=lists;
        this.roadBeanList=roadBeanList;
    }

    public void setSelected(boolean isSelect){
        this.isSelect=isSelect;
    }

    public List<String> getSelectList() {
        return selectList;
    }
    public List<PopBusinessRoadBean> getRoadList() {
        return roadBeanList;
    }

    @Override
    protected void convert(BaseViewHolder helper, BusinessBean item) {
        helper.setText(R.id.tv_Title,item.getTypeCname());
        if (helper.getItemViewType()==1){
            NoScrollGridView gridView=helper.getView(R.id.gridView);
            BusinessSelectAdapter adapter=new BusinessSelectAdapter(context,
                    R.layout.item_business_list,item.getSubTypes());
            if (selectList!=null){
                adapter.selectData(selectList);
            }
            adapter.setSelected(isSelect);
            adapter.getBusiness(new BusinessSelectAdapter.BusinessCallback() {
                @Override
                public void addBusiness(String str) {
                    if (selectList==null){
                        selectList=new ArrayList<String>();
                    }
                    if ("positiveInfo".equals(str)||"rectificativePerson".equals(str)){
                        selectList.remove("positiveInfo");
                        selectList.remove("rectificativePerson");
                        selectList.add(str);
                        notifyDataSetChanged();
                    }else {
                        selectList.add(str);
                    }
                }
                @Override
                public void deleteBusiness(String str) {
                    selectList.remove(str);
                }
            });
            gridView.setAdapter(adapter);
        }

        if (helper.getItemViewType()==2){
            InnerListView  listView=helper.getView(R.id.listView);
            BusinessSelect2Adapter adapter2=new BusinessSelect2Adapter(context,
                    R.layout.item_business_list2,item.getSubTypes());
            if (roadBeanList!=null){
                adapter2.selectData(roadBeanList);
            }
            adapter2.setSelected(isSelect);
            adapter2.getBusiness(new BusinessSelect2Adapter.BusinessCallback() {
                @Override
                public void addBusiness(PopBusinessRoadBean roadBean) {
                    if (roadBeanList==null){
                        roadBeanList=new ArrayList<PopBusinessRoadBean>();
                    }
                    for (PopBusinessRoadBean bean:roadBeanList){
                        if (roadBean.getType().equals(bean.getType())){
                            roadBeanList.remove(bean);
                            break;
                        }
                    }
                    if (!roadBean.getOrgId().equals("-1")){
                        roadBeanList.add(roadBean);
                    }
                }
            });
            listView.setAdapter(adapter2);
        }
    }
}
